#!/bin/sh
# shellcheck shell=dash disable=SC2169,SC3010,SC3015 source=/dev/null
#
# Startup script to ensure we have a valid filesystem
#

init_system() {

  CFG_TEMPLATE_DIR=/etc/config_templates

  # check if we have booted in LAN gateway mode
  if [[ -e /usr/local/HMLGW ]]; then
    HM_MODE="HM-LGW"
  fi

  # ensure some pathes are there and have
  # correct permissions
  mkdir -p /var/log
  mkdir -p /var/tmp
  mkdir -p /var/rega
  mkdir -p /var/run
  mkdir -p /var/spool
  mkdir -p /var/lock
  mkdir -p /var/cache
  mkdir -p /var/lib
  mkdir -p /var/lib/misc
  mkdir -p /var/lib/dbus 2>/dev/null
  mkdir -p /var/empty
  mkdir -p /var/etc
  mkdir -p /var/status
  chown root:status /var/status
  chmod 2775 /var/status
  mkdir -p /var/empty
  chmod 0700 /var/empty

  # /usr/local/etc setup
  mkdir -p /usr/local/etc
  chmod 0755 /usr/local/etc
  chown root:root /usr/local/etc

  # create /usr/local/etc/logrotate.d
  mkdir -p /usr/local/etc/logrotate.d
  chmod 0700 /usr/local/etc/logrotate.d
  chown root:root /usr/local/etc/logrotate.d

  # create a /usr/local/tmp directory for creating things like backup files, etc.
  # (we need it on-disk or /tmp will be not enough in case of a filled up /usr/local)
  if [[ ! -f /usr/local/tmp/.nobackup ]]; then
    mkdir -p /usr/local/tmp
    touch /usr/local/tmp/.nobackup
  fi
  # remove everything but keep dotfiles (e.g. '.nobackup')
  rm -rf /usr/local/tmp/*

  # make sure /usr/local/etc/config and
  # /usr/local/etc/config/firmware exists
  mkdir -p /usr/local/etc/config/firmware
  chmod 0775 /usr/local/etc/config
  chown root:root /usr/local/etc/config

  # make sure the timezone setup is fine
  if [[ ! -e /etc/config/TZ ]]; then
    cp -a ${CFG_TEMPLATE_DIR}/TZ /etc/config/
  fi

  # update /etc/config/localtime and /etc/config/timezone
  /bin/updateTZ.sh

  # if /etc/config/timezone is set to Europe/Berlin we
  # assume a german keyboard layout
  if [[ ! "${HM_HOST}" =~ oci\|lxc ]] &&
     [[ $(cat /etc/config/timezone) == "Europe/Berlin" ]]; then
    /sbin/loadkmap </etc/de.kmap
  fi

  # if no network config is present we make sure to put a template file
  # there
  if [[ ! -e /etc/config/netconfig ]] ; then
    cp -a ${CFG_TEMPLATE_DIR}/netconfig /etc/config/
  fi

  # if no shadow file with password information is in place or no
  # root user if found in shadow file we have to put the template
  # file there and ensure proper permissions
  if [[ ! -s /etc/config/shadow ]] ||
     ! grep -q "^root:" /etc/config/shadow; then
    cp -a ${CFG_TEMPLATE_DIR}/shadow /etc/config/
  fi
  chmod 0640 /etc/config/shadow
  chown root:root /etc/config/shadow

  # load bcm2835 watchdog kernel module if this is
  # a raspberrypi
  if echo "${HM_HOST}" | grep -q rpi; then
    modprobe bcm2835_wdt nowayout=1 heartbeat=15
  fi
}

start() {
  echo -n "Initializing System: "

  # source all data from /var/hm_mode
  [[ -r /var/hm_mode ]] && . /var/hm_mode

  # call rc.init before initializing the system
  if [[ ! -e /etc/config/safemode ]]; then
    if [[ -x /usr/local/etc/rc.init ]]; then
      echo -n "rc.init, "
      /usr/bin/timeout 120 /usr/local/etc/rc.init
    fi
  fi

  # make sure the system is properly initialized
  init_system

  # start hss_led if it exists and we are not in HMLGW mode
  if [[ "${HM_MODE}" != "HM-LGW" ]] && [[ -x /bin/hss_led ]]; then
    if [[ ! "${HM_HOST}" =~ oci\|lxc ]]; then
      start-stop-daemon -S -q -b -m -c hssled:hssled -p /var/run/hss_led.pid --exec /bin/hss_led -- -l 6
    else
      start-stop-daemon -S -q -b -m -p /var/run/hss_led.pid --exec /bin/hss_led -- -l 6
    fi
  fi

  # call rc.postinit after init of system is finished
  if [[ ! -e /etc/config/safemode ]]; then
    if [[ -x /usr/local/etc/rc.postinit ]]; then
      echo -n "rc.postinit, "
      /usr/bin/timeout 120 /usr/local/etc/rc.postinit
    fi
  fi

  # save all HM_ env variables
  set | grep '^HM_' >/var/hm_mode

  echo "OK"
}

stop() {
  echo -n "Cleaning up System: "

  if [[ -f /var/run/hss_led.pid ]]; then
    start-stop-daemon -K -q -p /var/run/hss_led.pid
  fi

  echo "OK"
}

restart() {
  stop
  start
}

case "$1" in
  start)
    start
  ;;
  stop)
    stop
  ;;
  restart|reload)
    restart
  ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac

exit 0
